<?php
/**
 * 二维码生成器
 * 使用phpqrcode库生成真正的二维码
 */

// 引入phpqrcode库
require_once __DIR__ . '/../libs/phpqrcode/qrlib.php';

/**
 * 生成邀请二维码
 * @param string $invite_code 邀请码
 * @param string $username 用户名
 * @param int $size 二维码尺寸
 * @return string 二维码的base64编码
 */
function generateInviteQRCode($invite_code, $username, $size = 200) {
    // 构建邀请链接
    $invite_url = "http://" . $_SERVER['HTTP_HOST'] . "/register.php?invite=" . urlencode($invite_code);
    
    return generateQRCodeAsBase64($invite_url, $size);
}


/**
 * 生成二维码并返回base64编码
 * @param string $text 要编码的文本
 * @param int $size 图片尺寸
 * @return string base64编码的图片或false
 */
function generateQRCodeAsBase64($text, $size = 200) {
    try {
        // 检查库是否已加载
        if (!class_exists('QRcode')) {
            error_log("QRcode类未找到，尝试重新加载库");
            require_once __DIR__ . '/../libs/phpqrcode/qrlib.php';
        }
        
        if (!class_exists('QRcode')) {
            error_log("QRcode类仍然未找到");
            return false;
        }
        
        // 清理输出缓冲，避免影响二维码生成
        while (ob_get_level()) {
            ob_end_clean();
        }
        
        // 使用phpqrcode库生成二维码 - 使用与测试文件相同的参数
        $png_data = QRcode::png($text, false, QR_ECLEVEL_L, 3, 4);
        
        if ($png_data && strlen($png_data) > 0) {
            // 检查是否是有效的PNG数据
            if (substr($png_data, 0, 8) === "\x89PNG\r\n\x1a\n") {
                $base64_data = base64_encode($png_data);
                return 'data:image/png;base64,' . $base64_data;
            } else {
                error_log("生成的不是有效的PNG数据");
                return false;
            }
        }
        
        error_log("生成二维码失败");
        return false;
        
    } catch (Exception $e) {
        error_log("生成二维码异常: " . $e->getMessage());
        return false;
    } catch (Error $e) {
        error_log("生成二维码错误: " . $e->getMessage());
        return false;
    }
}


/**
 * 生成带样式的邀请二维码
 * @param string $invite_code 邀请码
 * @param string $username 用户名
 * @param int $size 二维码尺寸
 * @return string 二维码的base64编码
 */
function generateStyledInviteQRCode($invite_code, $username, $size = 300) {
    // 构建邀请链接
    $invite_url = "http://" . $_SERVER['HTTP_HOST'] . "/register.php?invite=" . urlencode($invite_code);
    
    try {
        // 检查库是否已加载
        if (!class_exists('QRcode')) {
            require_once __DIR__ . '/../libs/phpqrcode/qrlib.php';
        }
        
        if (!class_exists('QRcode')) {
            return false;
        }
        
        // 清理输出缓冲，避免影响二维码生成
        while (ob_get_level()) {
            ob_end_clean();
        }
        
        // 生成基础二维码 - 使用与测试文件相同的参数
        $png_data = QRcode::png($invite_url, false, QR_ECLEVEL_L, 3, 4);
        
        if (!$png_data || strlen($png_data) < 100) {
            return false;
        }
        
        // 创建带样式的画布
        $canvas_size = $size + 100;
        $canvas = imagecreate($canvas_size, $canvas_size);
        
        // 定义颜色
        $white = imagecolorallocate($canvas, 255, 255, 255);
        $black = imagecolorallocate($canvas, 0, 0, 0);
        $blue = imagecolorallocate($canvas, 102, 126, 234);
        $purple = imagecolorallocate($canvas, 118, 75, 162);
        $gray = imagecolorallocate($canvas, 128, 128, 128);
        
        // 填充渐变背景
        for ($i = 0; $i < $canvas_size; $i++) {
            $ratio = $i / $canvas_size;
            $r = 102 + (118 - 102) * $ratio;
            $g = 126 + (75 - 126) * $ratio;
            $b = 234 + (162 - 234) * $ratio;
            $color = imagecolorallocate($canvas, $r, $g, $b);
            imageline($canvas, 0, $i, $canvas_size, $i, $color);
        }
        
        // 添加白色背景
        $margin = 20;
        imagefilledrectangle($canvas, $margin, $margin, 
            $canvas_size - $margin, $canvas_size - $margin, $white);
        
        // 加载二维码图片
        $qr_image = imagecreatefromstring($png_data);
        if ($qr_image) {
            // 将二维码复制到画布中央
            $qr_margin = 50;
            $qr_x = $margin + $qr_margin;
            $qr_y = $margin + $qr_margin;
            $qr_display_size = $size - 2 * $qr_margin;
            
            imagecopyresampled($canvas, $qr_image, $qr_x, $qr_y, 0, 0, 
                $qr_display_size, $qr_display_size, $size, $size);
            imagedestroy($qr_image);
        }
        
        // 添加标题文字
        $title = "投注游戏邀请";
        $font_size = 5;
        $title_width = strlen($title) * imagefontwidth($font_size);
        $title_x = ($canvas_size - $title_width) / 2;
        $title_y = $margin + 20;
        imagestring($canvas, $font_size, $title_x, $title_y, $title, $black);
        
        // 添加用户名
        $user_text = "邀请人: " . $username;
        $user_width = strlen($user_text) * imagefontwidth(3);
        $user_x = ($canvas_size - $user_width) / 2;
        $user_y = $title_y + 25;
        imagestring($canvas, 3, $user_x, $user_y, $user_text, $gray);
        
        // 添加邀请码
        $code_text = "邀请码: " . $invite_code;
        $code_width = strlen($code_text) * imagefontwidth(3);
        $code_x = ($canvas_size - $code_width) / 2;
        $code_y = $user_y + 20;
        imagestring($canvas, 3, $code_x, $code_y, $code_text, $blue);
        
        // 添加说明文字
        $desc_text = "扫码注册获得奖励";
        $desc_width = strlen($desc_text) * imagefontwidth(2);
        $desc_x = ($canvas_size - $desc_width) / 2;
        $desc_y = $canvas_size - $margin - 15;
        imagestring($canvas, 2, $desc_x, $desc_y, $desc_text, $gray);
        
        // 输出为base64
        ob_start();
        imagepng($canvas);
        $image_data = ob_get_contents();
        ob_end_clean();
        imagedestroy($canvas);
        
        $base64_data = base64_encode($image_data);
        return 'data:image/png;base64,' . $base64_data;
        
    } catch (Exception $e) {
        error_log("生成样式二维码失败: " . $e->getMessage());
        return false;
    }
}


?>
